package com.jiantong.web.open.app.user;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.dto.BreedFactoryDto;
import com.jiantong.user.dto.RetailBreedFactoryDto;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.User;
import com.jiantong.user.model.UserProfile;
import com.jiantong.user.service.*;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.service.RetailBreedRecordReadService;
import com.jiantong.work.service.RetailImmunizationRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 养殖档案,养殖地图,养殖列表
 * Created by mz03 on 2017/3/20.
 * @author :zyp
 */
@Slf4j
@OpenBean
public class BreedFactoryApis {
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @RpcConsumer
    private BreedFactoryWriteService breedFactoryWriteService;

    @RpcConsumer
    private UserReadService<User> userReadService;

    @RpcConsumer
    private UserProfileReadService userProfileReadService;

    @RpcConsumer
    private RetailBreedFactoryWriteService retailBreedFactoryWriteService;

    @RpcConsumer
    private RetailBreedRecordReadService retailBreedRecordReadService;

    @RpcConsumer
    private RetailImmunizationRecordReadService retailImmunizationRecordReadService;


    /**
     * 根据ID查询全部
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.breedFactory.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedFactory findById(Long id){
        Response<BreedFactory> recordResponse = breedFactoryReadService.findById(id);
        if(!recordResponse.isSuccess()){
            log.error("failed to find BreedFactory read by id = {}, cause : {}", id, recordResponse.getError());
            throw new OPServerException(recordResponse.getError());
        }
        return recordResponse.getResult();
    }

    /**
     * 根据ID查询User中的一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "test.find.user.User.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public User findUserByid(Long id){
            Response<User> user = userReadService.findById(id);
            if (!user.isSuccess()){
                log.error("failed to find BreedFactory read by id = {}, cause : {}", id, user.getError());
                throw new OPServerException(user.getError());
            }
            return user.getResult();
    }


    /**
     * 根据状态(status)查询当前登录人管理的所有养殖场
     * @param status
     * @return
     */
    @OpenMethod(
            key = "find.breedRecordList.by.status",
            paramNames = {"status"},
            httpMethods = RequestMethod.GET
    )
    public List<BreedFactory> findByStatus(Integer status){
        Response<List<BreedFactory>> response = breedFactoryReadService.findByStatus(status);
        if(!response.isSuccess()){
            log.error("failed to find BreedFactoryList by type = {}, cause : {}",status,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 添加一条
     * @param breedFactory
     * @return
     */
    @OpenMethod(
            key = "create.breedRecord",
            paramNames = {"breedRecord"},
            httpMethods = RequestMethod.POST
    )
    public Long create(BreedFactory breedFactory){

        Response<Long> response = breedFactoryWriteService.create(breedFactory);
        if(!response.isSuccess()){
            log.error("failed to create breedRecord read by breedRecord = {}, cause : {}",breedFactory,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据ID查询全部,并且加上用户的详情
     * @param id
     * @return BreedFactoryDto
     */
    @OpenMethod(
            key = "find.breedFactoryDto.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedFactoryDto findBreedFactoryDtoById(Long id){
        //拿到recordResponse的信息
        Response<BreedFactory> recordResponse = breedFactoryReadService.findById(id);
        if (!recordResponse.isSuccess()){
            log.error("failed to find BreedFactory read by id = {}, cause : {}", id, recordResponse.getError());
            throw new OPServerException(recordResponse.getError());
        }
        BreedFactoryDto breedFactoryDto = new BreedFactoryDto();
        breedFactoryDto.setBreedFactory(recordResponse.getResult());
        //拿到user的详细信息
        Response<User> userResponse = userReadService.findUserByUserId(recordResponse.getResult().getUserId());
        Response<UserProfile> userProfileResponse = userProfileReadService.findUserProfileById(recordResponse.getResult().getUserId());
        if (!userResponse.isSuccess() || !userProfileResponse.isSuccess()) {
            log.error("failed to find user read by userid = {}, cause : {}", id, recordResponse.getError());
            throw new OPServerException(userResponse.getError());
        }
        breedFactoryDto.setUser(userResponse.getResult());
        breedFactoryDto.setRealName(userProfileResponse.getResult().getRealName());

        return breedFactoryDto;
    }


    /**
     * 分页查询 养殖场并携带养殖场主信息
     * @return Paging<BreedFactoryDto>
     */
    @OpenMethod(
            key = "find.paging.BreedFactoryDto.by.status",
            paramNames = {"pageNo","pageSize","status"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedFactoryDto> pagingBreedRecord(Integer pageNo,Integer pageSize,Integer status) {

        Response<Paging<BreedFactory>> pagingResponse = breedFactoryReadService.pagingBreedRecord(pageNo, pageSize, status);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find BreedFactoryList by type = {}, cause : {}",status,pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        List<Long> longs = new ArrayList<>();

        //拿到这一页的---- breedFactorys
        List<BreedFactory> breedFactories = pagingResponse.getResult().getData();
        try {
            //根据这一组breedFactory查出来user_id
            if (!breedFactories.isEmpty()) {
                for (int i = 0; i < breedFactories.size(); i++) {
                    longs.add(i, breedFactories.get(i).getUserId());
                }
            }
        }catch (Exception e){
            log.error("failed get breeDFactoryUser_id cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException("find userId failed");
        }

        //根据List<user_id>查询出List<User> ---- userList
        List<User> userList = new ArrayList<>();
        try {
            if (!longs.isEmpty()) {
                Response<List<User>> userResponse = userReadService.findByIds(longs);
                userList = userResponse.getResult();
            }
        }catch (Exception e){
            log.error("failed structure userList cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException("find userIds by userId failed");
        }

        //利用List<BreedFactory>和List<User> 组合一个List<BreedFactoryDto>
        List<BreedFactoryDto> breedFactoryDtoList = new ArrayList<>();
        BreedFactory bf = new BreedFactory();
        User us = new User();
        try {
            if (!breedFactories.isEmpty() && (breedFactories.size() == userList.size())){
                for (int i = 0; i < breedFactories.size(); i++) {
                    //拿到单个的BreedFactory和User 并判断id和user_id对应的一组组成一个BreedFactoryDto
                    bf = breedFactories.get(i);
                    for (int j = 0; j<breedFactories.size();j++){
                        us = userList.get(j);
                        if (us.getId().equals(bf.getUserId())){
                            BreedFactoryDto bfd = new BreedFactoryDto();
                            bfd.setBreedFactory(bf);
                            bfd.setUser(us);
                            breedFactoryDtoList.add(i, bfd);
                            break;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("failed structure breedFactoryDtoList cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException("create breedFactoryDto failed");
        }

        //把List<BreedFactoryDto>转为Paging<BreedFactoryDto>并返回
        Paging<BreedFactoryDto> paging = new Paging<>();
        paging.setData(breedFactoryDtoList);
        paging.setTotal(pagingResponse.getResult().getTotal());
        return  paging;
    }

    /**
     * 利用关键字 根据农场名\地区 分页当前登录人管理的所有养殖场 并携带农场主信息
     * @param pageNo
     * @param pageSize
     * @param keyword
     * @return
     */
    @OpenMethod(
            key = "find.findBreedFactoryDto.by.keyword",
            paramNames = {"pageNo","pageSize","keyword"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedFactoryDto> findBreedFactoryDtoByKeyword(Integer pageNo,Integer pageSize,String keyword){
        try {

            Paging<BreedFactoryDto> pagingBreedFactoryDto = new Paging<>();
            List<BreedFactory> breedFactoryList = new ArrayList<>();           //存放BreedFactory
            List<BreedFactoryDto> breedFactoryDtoList = new ArrayList<>();      //存放BreedFactoryDto
            List<User> userList = new ArrayList<>();                      //存放user
            List<Long> listUserId = new ArrayList<>();                   //存放user_id

            //获得 breedFactoryList
            Response<Paging<BreedFactory>> pagingResponse = breedFactoryReadService.pagingBreedRecordByKeyword(pageNo, pageSize, keyword);
            if (!pagingResponse.isSuccess()) {
                log.error("failed to find Paging<BreedFactoryDto> by keyword = {}, cause : {}", keyword, pagingResponse.getError());
                throw new OPServerException(pagingResponse.getError());
            }
            Paging<BreedFactory> pagingBreedFactory = pagingResponse.getResult();
            breedFactoryList = pagingBreedFactory.getData();

            // 根据breedFactoryList 得到 userList
           // List<BreedFactory> listBreedFactory = pagingBreedFactory.getData();
            for (int i = 0; i < breedFactoryList.size(); i++) {
                listUserId.add(i, breedFactoryList.get(i).getUserId());
            }
            Response<List<User>> userResponse = userReadService.findByIds(listUserId);
            userList = userResponse.getResult();

            //得到breedFactoryDtoList

            if (!userList.isEmpty() && (userList.size() == breedFactoryList.size())) {
                for (int i = 0; i < userList.size(); i++) {
                    BreedFactoryDto breedFactoryDto = new BreedFactoryDto();
                    breedFactoryDto.setUser(userList.get(i));
                    for (BreedFactory aBreedFactoryList : breedFactoryList) {
                        //当userId和BreedFactory的Id相等时才把两者传给dto
                        if (userList.get(i).getId().equals(aBreedFactoryList.getUserId())) {
                            breedFactoryDto.setBreedFactory(aBreedFactoryList);
                            breedFactoryDtoList.add(i, breedFactoryDto);
                            break;
                        }
                    }
                }
            }
            pagingBreedFactoryDto.setData(breedFactoryDtoList);
            return pagingBreedFactoryDto;
        }catch (Exception e){
            log.error("failed breedFactoryDtoList cause:{}",Throwables.getStackTraceAsString(e));
            return null;
        }
    }

    /**
     * 利用关键字对应用户名 分页模糊查询,并携带农场信息
     *
     * @param pageNo
     * @param pageSize
     * @param keyword
     * @return  Paging<BreedFactoryDto> 失败返回null
     */
    @OpenMethod(
            key = "find.findBreedFactoryDto.by.keyword.user.name",
            paramNames = {"pageNo","pageSize","name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedFactoryDto> findByKeywordName(Integer pageNo,Integer pageSize,String keyword){

        try {
            //拿到一组user  --  list<user>
            Response<Paging<User>> response = userReadService.findByKeywordName(pageNo, pageSize, keyword);
            if (!response.isSuccess()) {
                log.error("failed to find Paging<user> by keyword = {}, cause : {}", keyword, response.getError());
                throw new OPServerException(response.getError());
            }
            List<User> listUser = response.getResult().getData();

            //拿到listUser里的ID  listId
            List<Long> listId = new ArrayList<>();
            for (int i = 0; i < listUser.size(); i++) {
                listId.add(i, listUser.get(i).getId());
            }

            //根据ID对应养殖场的user_id 查询出listUser对应的一组养殖场
            Response<List<BreedFactory>> listResponse = breedFactoryReadService.findByUser_id(listId);
            if (!listResponse.isSuccess()) {
                log.error("failed to find List<BreedFactory> by listId = {}, cause : {}", listId.toString(), response.getError());
                throw new OPServerException(response.getError());
            }
            List<BreedFactory> breedFactoryList = listResponse.getResult();

            //根据List<User>和List<BreedFactory>组成一组List<BreedFactoryDto>
            List<BreedFactoryDto> breedFactoryDtoList = new ArrayList<>();
            if (!breedFactoryList.isEmpty() && (breedFactoryList.size() == listUser.size())) {
                for (BreedFactory breedFactory : breedFactoryList) {
                    BreedFactoryDto breedFactoryDto = new BreedFactoryDto();
                    for (User aListUser : listUser) {
                        if (breedFactory.getUserId().equals(aListUser.getId())) {
                            breedFactoryDto.setUser(aListUser);
                            breedFactoryDtoList.add(breedFactoryDto);
                            break;
                        }
                    }
                }
            }

            //造一个Paging<BreedFactoryDto> 返回出去
            Paging<BreedFactoryDto> paging = new Paging<>();
            paging.setData(breedFactoryDtoList);
            return paging;
        }catch (Exception e){
            log.error("failed breedFactoryDtoList cause:{}",Throwables.getStackTraceAsString(e));
            return null;
        }
    }

    /**
     * 根据省,市查询养殖场
     * @param provinceId
     * @param cityId
     * @return
     */
    @OpenMethod(
            key = "find.breedRecordList.by.cityId",
            paramNames = {"provinceId","cityId"},
            httpMethods = RequestMethod.GET
    )
    public List<BreedFactory> findByCityId(Long provinceId, Long cityId){
        Response<List<BreedFactory>> response = breedFactoryReadService.findByCityId(provinceId,cityId);
        if(!response.isSuccess()){
            log.error("failed to find BreedFactoryList by provinceId = {},cityId = {}, cause : {}",provinceId,cityId,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 养殖场各个种类存栏合计 根据种类类型查询存栏合计 如果类型为空查询全部类型的存栏合计
     * @param breedType
     * @return
     */
    @OpenMethod(
            key = "find.stockQuantityTotal.by.breedType",
            paramNames = {"breedType"},
            httpMethods = RequestMethod.GET
    )
    public Map<String,Integer> findStockQuantityByType(Integer breedType){
        Response<List<Integer>> response = breedFactoryReadService.findQuantityByType(breedType);
        if (!response.isSuccess()){
            log.error("failed to find BreedFactoryList by provinceId = {},breedType = {}, cause : {}",breedType,response.getError());
            throw new OPServerException(response.getError());
        }
        Map<String,Integer> map = Maps.newHashMap();
        for (int i=0; i<response.getResult().size();i++){
            switch (i+1){
                case 1:
                    map.put("pig",response.getResult().get(i));
                    break;
                case 3:
                    map.put("cow",response.getResult().get(i));
                    break;
                case 2:
                    map.put("sheep",response.getResult().get(i));
                    break;
                case 4:
                    map.put("poultry",response.getResult().get(i));
                    break;
            }
        }
        return map;
    }

    @OpenMethod(
            key = "add.retail.breed.factory",
            paramNames = {"retailBreedFactoryDto"},
            httpMethods = RequestMethod.POST
    )
    public Map<String , Object> createRetailBreedFactory(RetailBreedFactoryDto retailBreedFactoryDto){
        if(retailBreedFactoryDto.getUser() == null || retailBreedFactoryDto.getBreedFactory() == null
                || retailBreedFactoryDto.getRealName() == null){
            log.error("this object should be not null");
            throw new OPClientException(ExceptionCode.OBJECT_NOT_EXIST.value(),
                    "this object should be not null");
        }
        Response<Map<String , Object>> response =
                retailBreedFactoryWriteService.createRetailBreedFactory(retailBreedFactoryDto);

        if(!response.isSuccess()){
            log.error("failed to add retail breed factory");
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "update.retail.breed.factory",
            paramNames = {"retailBreedFactoryDto"},
            httpMethods = RequestMethod.PUT
    )
    public Map<String , Object> updateRetailBreedFactory(RetailBreedFactoryDto retailBreedFactoryDto){
        if(retailBreedFactoryDto.getUser().getId() == null
                || retailBreedFactoryDto.getBreedFactory().getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Map<String ,Object>> response =
                retailBreedFactoryWriteService.updateRetailBreedFactory(retailBreedFactoryDto);

        if(!response.isSuccess()){
            log.error("failed to update retail breed factory case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "paging.retail.breed.factory",
            paramNames = {"pageNo","pageSize","status","villageId","name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<RetailBreedFactoryDto> pagingRetail(Integer pageNo, Integer pageSize,Integer status,Long villageId,String name){
        Response<Paging<BreedFactory>> pagingResponse = breedFactoryReadService.pagingRetail(pageNo, pageSize,status,villageId,name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find BreedFactoryList, cause : {}",pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        List<Long> longs = new ArrayList<>();
        List<Long> ids = new ArrayList<>();

        //拿到这一页的---- breedFactories
        List<BreedFactory> breedFactories = pagingResponse.getResult().getData();
        try {
            //根据这一组breedFactory查出来user_id
            if (!breedFactories.isEmpty()) {
                for (int i = 0; i < breedFactories.size(); i++) {
                    longs.add(i, breedFactories.get(i).getUserId());
                    ids.add(i,breedFactories.get(i).getId());
                }
            }
        }catch (Exception e){
            log.error("failed get breeDFactoryUser_id cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException("find userId failed");
        }

        //根据List<user_id>查询出List<User> ---- userList
        List<User> userList = new ArrayList<>();
        List<UserProfile> userProfiles = new ArrayList<>();
        List<Boolean> isVaccine = new ArrayList<>();
        try {
            if (!longs.isEmpty()) {
                Response<List<User>> userResponse = userReadService.findByIds(longs);
                Response<List<UserProfile>> userProfileResponse = userProfileReadService.findUserIds(longs);
                Response<List<Boolean>> isVaccineResponse = retailImmunizationRecordReadService.countIsVaccine(ids);
                isVaccine = isVaccineResponse.getResult();
                userList = userResponse.getResult();
                userProfiles = userProfileResponse.getResult();
            }
        }catch (Exception e){
            log.error("failed structure userList cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException("find userIds by userId failed");
        }

        //利用List<BreedFactory>和List<User> 组合一个List<BreedFactoryDto>
        List<RetailBreedFactoryDto> retailBreedFactoryDtos = new ArrayList<>();
        BreedFactory bf = new BreedFactory();
        User us = new User();
        UserProfile userProfile = new UserProfile();
        try {
            if (!breedFactories.isEmpty() && (breedFactories.size() == userList.size())){
                for (int i = 0; i < breedFactories.size(); i++) {
                    //拿到单个的BreedFactory和User 并判断id和user_id对应的一组组成一个BreedFactoryDto
                    bf = breedFactories.get(i);
                    for (int j = 0; j<breedFactories.size();j++){
                        us = userList.get(j);
                        userProfile = userProfiles.get(j);
                        if (us.getId().equals(bf.getUserId())){
                            RetailBreedFactoryDto retailBreedFactoryDto = new RetailBreedFactoryDto();
                            retailBreedFactoryDto.setBreedFactory(bf);
                            retailBreedFactoryDto.setUser(us);
                            retailBreedFactoryDto.setRealName(userProfile.getRealName());
                            retailBreedFactoryDto.setIsVaccine(isVaccine.get(i));
                            retailBreedFactoryDtos.add(i, retailBreedFactoryDto);
                            break;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("failed retail breedFactoryDtoList cause:{}",Throwables.getStackTraceAsString(e));
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    "paging retail breed factory failed");
        }

        //把List<BreedFactoryDto>转为Paging<BreedFactoryDto>并返回
        Paging<RetailBreedFactoryDto> paging = new Paging<>();
        paging.setData(retailBreedFactoryDtos);
        paging.setTotal(pagingResponse.getResult().getTotal());
        return  paging;
    }


    @OpenMethod(
            key = "find.retail.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public RetailBreedFactoryDto findRetailById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<BreedFactory> response = breedFactoryReadService.findById(id);
        if(!response.isSuccess()){
            log.error("failed to find breed factory");
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    response.getError());
        }
        Response<User> userResponse = userReadService.findUserByUserId(response.getResult().getUserId());
        if(!userResponse.isSuccess()){
            log.error("failed to find user");
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    userResponse.getError());
        }
        Response<UserProfile> userProfileResponse =
                userProfileReadService.findUserProfileById(userResponse.getResult().getId());

        if(!userProfileResponse.isSuccess()){
            log.error("failed to find user profile");
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    userProfileResponse.getError());
        }

        RetailBreedFactoryDto retailBreedFactoryDto = new RetailBreedFactoryDto();

        retailBreedFactoryDto.setUser(userResponse.getResult());
        retailBreedFactoryDto.setBreedFactory(response.getResult());
        retailBreedFactoryDto.setRealName(userProfileResponse.getResult().getRealName());

        return retailBreedFactoryDto;
    }

    @OpenMethod(
            key = "sum.stock.quantity",
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , Object>> sumStockQuantity(){
        LoginUser loginUser = UserUtil.getCurrentUser();
        List<Long> ids = loginUser.getBreedFactoryIds();
        Response<List<Map<String , Object>>> response = breedFactoryReadService.sumStockQuantity(ids);
        if(!response.isSuccess()){
            log.error("failed to sum stock quantity");
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据当前登录人拿到其管辖的所有村子的map
     * @return map<村子id,村子name>
     */
    @OpenMethod(
            key = "find.all.village.by.ids",
            httpMethods = RequestMethod.GET
    )
    public Map<Integer , Object> allVillage(){
        LoginUser loginUser = UserUtil.getCurrentUser();
        List<Long> ids = loginUser.getBreedFactoryIds();
        Response<List<Map<String , Object>>> response = breedFactoryReadService.allVillageByIds(ids);
        if(!response.isSuccess()){
            log.error("failed to all village");
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    response.getError());
        }
        Map<Integer,Object> villageMap = Maps.newHashMap();
        for (Map<String ,Object> map: response.getResult()) {
            villageMap.put(Integer.parseInt(String.valueOf(map.get("villageId"))),map.get("village"));
        }
        return villageMap;
    }

    /**
     * 根据传来的村子id 拿到这个村子下的所有养殖场id
     */
}
